package io.renren.modules.paper.controller;

import io.renren.common.utils.R;
import io.renren.modules.paper.entity.Paper;
import io.renren.modules.paper.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Yang
 * @Date 2019/9/4 18:48
 */
@RestController
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    private PaperService paperService;

    /***
     * 查询全部试卷
     * @author Yang
     * @date 2019/9/4 19:04
     * @return : io.renren.common.utils.R
     */
    @GetMapping("/findByAll")
    public R findByAll(){
        final List<Map<String, Object>> list = paperService.listMaps();
        return R.ok().put("list",list);
    }

    /***
     * 通过id查询试卷问题详情
     * @author Yang
     * @date 2019/9/5 14:55
     * @param paperId :
     * @return : io.renren.common.utils.R
     */
    @GetMapping("/getPaperByPaperId")
    public R getPaperByPaperId(@RequestParam("paperId")Integer paperId){
        final Paper byId = paperService.getById(paperId);
        return R.ok().put("byId", byId);
    }

    /***
     * 删除选中试卷信息
     * @author Yang
     * @date 2019/9/5 18:42
     * @param obj :
     * @return : io.renren.common.utils.R
     */
    @PostMapping(value = "/deletePaper")
    public R deletePaper(@RequestBody Map<String, Object> obj){
        Integer paperId = (Integer) obj.get("paperId");
        Paper selectResult = paperService.getById(paperId);
        if(selectResult != null) {
            return R.error("该试卷已有学生参加考试，删除失败");
        } else {
            boolean deleteResult = paperService.removeById(paperId);
            if(deleteResult){
                return R.ok().put("删除成功",null);
            } else {
                return R.error("数据库错误，删除失败");
            }
        }
    }

}
